{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Train an Expert Agent in AGiXT with ezLocalai Provider\n",
    "\n",
    "This example assumes that you have your AGiXT and ezLocalai servers set up and running. This example is specifically for running AGiXT with ezLocalai and training a local agent. If you do not wish to run local models, we have the same example set up for using an agent with the OpenAI API.\n",
    "\n",
    "If you do not have ezLocalai set up and you want to set it up to run local models, go to https://github.com/DevXT-LLC/ezlocalai and follow the instructions there to set up the server, then continue with this example.\n",
    "\n",
    "## Create the Agent\n",
    "\n",
    "For this example, we will create an expert on AGiXT. These settings can be easily changed in the streamlit app or over API.\n",
    "\n",
    "Modify the `agixt_server`, `api_key`, `agent_name`, `EZLOCALAI_API_URL`, `EZLOCALAI_API_KEY`, `persona`, and any others as needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agixtsdk import AGiXTSDK\n",
    "\n",
    "agixt_server = \"http://localhost:7437\"  # Change this to your AGiXT server URL\n",
    "\n",
    "\n",
    "api_key = \"None\"  # Change this to your AGiXT API key (This should be your JWT in the web interface)\n",
    "agixt = AGiXTSDK(base_uri=agixt_server, api_key=api_key)\n",
    "\n",
    "# If you dont have your jwt but have your authenticator:\n",
    "# agixt = AGiXTSDK(base_uri=agixt_server)\n",
    "# agixt.login(email=\"Your email address\", otp=\"123456\")\n",
    "\n",
    "\n",
    "agent_name = \"AGiXT\"  # Change this if desired\n",
    "\n",
    "\n",
    "agixt.add_agent(\n",
    "    agent_name=agent_name,\n",
    "    settings={\n",
    "        \"provider\": \"rotation\",\n",
    "        \"vision_provider\": \"rotation\",\n",
    "        \"tts_provider\": \"ezlocalai\",\n",
    "        \"transcription_provider\": \"default\",\n",
    "        \"translation_provider\": \"default\",\n",
    "        \"embeddings_provider\": \"default\",\n",
    "        \"image_provider\": \"None\",\n",
    "        \"ANTHROPIC_API_KEY\": \"\",\n",
    "        \"ANTHROPIC_MODEL\": \"claude-3-5-sonnet-20241022\",\n",
    "        \"AZURE_MODEL\": \"\",\n",
    "        \"AZURE_API_KEY\": \"\",\n",
    "        \"AZURE_OPENAI_ENDPOINT\": \"\",\n",
    "        \"AZURE_DEPLOYMENT_NAME\": \"\",\n",
    "        \"AZURE_TEMPERATURE\": 0.7,\n",
    "        \"AZURE_TOP_P\": 0.95,\n",
    "        \"DEEPSEEK_API_KEY\": \"\",\n",
    "        \"DEEPSEEK_MODEL\": \"deepseek-chat\",\n",
    "        \"GOOGLE_API_KEY\": \"\",\n",
    "        \"GOOGLE_MODEL\": \"gemini-exp-1206\",\n",
    "        \"GOOGLE_TEMPERATURE\": 0.7,\n",
    "        \"GOOGLE_TOP_P\": 0.95,\n",
    "        \"EZLOCALAI_API_URI\": \"http://ezlocalai:8091/v1/\",  # URL for the EZLOCALAI API, change this to your EZLOCALAI API URL. Never use localhost here, it is a different container.\n",
    "        \"EZLOCALAI_API_KEY\": \"Your EZLOCALAI API key\",  # Change this to your EZLOCALAI API key\n",
    "        \"EZLOCALAI_VOICE\": \"DukeNukem\",  # Voice for TTS, change this to the voice you want to use from ezlocalai.\n",
    "        \"EZLOCALAI_TEMPERATURE\": 1.33,\n",
    "        \"EZLOCALAI_TOP_P\": 0.95,\n",
    "        \"OPENAI_API_KEY\": \"\",  # Enter your OpenAI API key here\n",
    "        \"OPENAI_MODEL\": \"chatgpt-4o-latest\",\n",
    "        \"XAI_API_KEY\": \"\",\n",
    "        \"XAI_MODEL\": \"grok-beta\",\n",
    "        \"EZLOCALAI_MAX_TOKENS\": \"1\",\n",
    "        \"DEEPSEEK_MAX_TOKENS\": \"60000\",\n",
    "        \"AZURE_MAX_TOKENS\": \"100000\",\n",
    "        \"XAI_MAX_TOKENS\": \"120000\",\n",
    "        \"OPENAI_MAX_TOKENS\": \"128000\",\n",
    "        \"ANTHROPIC_MAX_TOKENS\": \"200000\",\n",
    "        \"GOOGLE_MAX_TOKENS\": \"2097152\",\n",
    "        \"SMARTEST_PROVIDER\": \"anthropic\",\n",
    "        \"mode\": \"prompt\",\n",
    "        \"prompt_name\": \"Think About It\",\n",
    "        \"prompt_category\": \"Default\",\n",
    "        \"analyze_user_input\": False,\n",
    "        \"websearch\": False,\n",
    "        \"websearch_depth\": 2,\n",
    "        \"WEBSEARCH_TIMEOUT\": 0,\n",
    "        \"persona\": \"AGiXT is an expert on the AGiXT AI agent automation platform and supports the users of AGiXT.\",  # Use this field to set persona for the AI model\n",
    "        \"tts\": False,\n",
    "    },\n",
    "    training_urls=[],  # Add training URLs here if you want to train the agent\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Zip your training data\n",
    "\n",
    "Creates a zip file called `training_data.zip` of the AGiXT `docs` folder. You can change this to any folder that you would like to use as training data, or skip this step and use an existing zip file.\n",
    "\n",
    "A good example of what to use for training data would be any PDF, word document, text file, or any other kind of file with information in it that you would like the agent to learn from."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from zipfile import ZipFile\n",
    "import os\n",
    "\n",
    "os.chdir(\"../\")\n",
    "with ZipFile(\"examples/training_data.zip\", \"w\") as zipObj:\n",
    "    for foldername, subfolders, filenames in os.walk(\"docs\"):\n",
    "        for filename in filenames:\n",
    "            file_path = os.path.join(foldername, filename)\n",
    "            zipObj.write(file_path)\n",
    "os.chdir(\"examples/\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Train the Agent on the training data\n",
    "\n",
    "This will train the agent on the training data that you have provided. This will take some time to complete depending on the size of the training data. A zip file around 70MB in size takes around 3 minutes to complete. The AGiXT docs should complete very quickly since it is all markdown files totaling around 3MB."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import base64\n",
    "\n",
    "zip_file_name = \"training_data.zip\"\n",
    "training_data = base64.b64encode(open(zip_file_name, \"rb\").read()).decode(\"utf-8\")\n",
    "\n",
    "agixt.learn_file(\n",
    "    agent_name=agent_name,\n",
    "    file_name=zip_file_name,\n",
    "    file_content=training_data,\n",
    "    collection_number=\"0\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chat with your trained expert AGiXT agent\n",
    "\n",
    "AGiXT has direct support for using the OpenAI API for chat completions. See this link for more information to take advantage of the abilities of this endpoint: https://josh-xt.github.io/AGiXT/2-Concepts/04-Chat%20Completions.html\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "\n",
    "prompt = \"What can you tell me about AGiXT?\"\n",
    "\n",
    "openai.base_url = f\"{agixt_server}/v1/\"\n",
    "openai.api_key = api_key\n",
    "openai.api_type = \"openai\"\n",
    "\n",
    "response = openai.chat.completions.create(\n",
    "    model=agent_name,\n",
    "    messages=[{\"role\": \"user\", \"content\": prompt}],\n",
    "    user=\"Tell me about AGiXT\",  # This field is used for the conversation name, if empty, it will use today's date\n",
    ")\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# That is all! \n",
    "\n",
    "You now have a trained expert agent in AGiXT. This agent will be able to support users by answering questions, providing information, and more about AGiXT."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
